import { SupportedMimeTypes } from './supported-mime-types'

export enum ErrorCodes {
  UnknownError,
  InternalServerError,
  RateLimited,
  NotFound,
  Forbidden,
  BadRequest,
  Unauthorized,
  RpcError,
  WsError,
  ThirdPartyFail,
  UnknownUser,
  ShortLinkTaken,
  UnknownShortDomain,
  UnknownLibrary,
  TokenInvalid,
  UnknownResource,
  UnknownTag,
  PermissionDenied,
  UnknownComment,
  UnknownBot,
  UnknownServer,
  UnknownBadge,
  UserAlreadyHasABadge,
  InvalidInviteCode,
  ServerAlreadyExists,
  BotNotPresentOnQueueServer,
  UnknownUp,
  TooManyUps,
  InvalidStatus,
  UnknownReport,
  UnsupportedMediaType,
  UnknownApplication,
  AutomatedRequestsNotAllowed,
  InvalidRating,
  DuplicateBot,
  CannotDetectIp,
  ThirdPartyMonitoringFail,
  ThirdPartyMonitoringNotApproved,
  TurnstileError,
  ServiceConfiguredIncorrectly,
  UnknownBoost,
  BoostExpired,
  AlreadyCommented,
  ReviewRatingsConflict,
  TooManyConsecutiveMessages,
  NoServiceBot,
  AlreadyReported,
  CannotDeleteOwner,
  BlockedBot,
  UnknownAutomation,
  OnlyOwnerCanAddServer,
  ReviewReported,
}

export const errorMessages: Record<ErrorCodes, string> = {
  [ErrorCodes.InternalServerError]: 'Internal server error',
  [ErrorCodes.RateLimited]: 'RateLimited',
  [ErrorCodes.NotFound]: 'Not found',
  [ErrorCodes.Forbidden]: 'Forbidden',
  [ErrorCodes.Unauthorized]: 'Unauthorized',
  [ErrorCodes.BadRequest]: 'Bad request',  
  [ErrorCodes.RpcError]: 'Rpc error',
  [ErrorCodes.WsError]: 'Websocket error',
  [ErrorCodes.UnknownError]: 'Unknown error',
  [ErrorCodes.ThirdPartyFail]: 'Third party failed',
  [ErrorCodes.UnknownUser]: 'Unknown user',
  [ErrorCodes.ShortLinkTaken]: 'Short link was already taken',
  [ErrorCodes.UnknownShortDomain]: 'Unknown short domain',
  [ErrorCodes.UnknownLibrary]: 'Unknown library',
  [ErrorCodes.TokenInvalid]: 'Token invalid',
  [ErrorCodes.UnknownResource]: 'Unknown resource',
  [ErrorCodes.UnknownTag]: 'Unknown tag',
  [ErrorCodes.PermissionDenied]: 'Permission denied: lack of permissions',
  [ErrorCodes.UnknownComment]: 'Unknown comment',
  [ErrorCodes.UnknownBot]: 'Unknown bot',
  [ErrorCodes.UnknownServer]: 'Unknown server',
  [ErrorCodes.UnknownBadge]: 'Unknown badge',
  [ErrorCodes.UserAlreadyHasABadge]: 'User already has a badge',
  [ErrorCodes.InvalidInviteCode]: 'Invalid invite code',
  [ErrorCodes.ServerAlreadyExists]: 'Server already exists',
  [ErrorCodes.BotNotPresentOnQueueServer]: 'Bot is not present on queue server',
  [ErrorCodes.UnknownUp]: 'Unknown up',
  [ErrorCodes.TooManyUps]: 'Too many ups',
  [ErrorCodes.InvalidStatus]: 'Invalid status',
  [ErrorCodes.UnknownReport]: 'Unknown report',
  [ErrorCodes.UnsupportedMediaType]: `Unsupported media type, should be one of ${SupportedMimeTypes.join(', ')}`,
  [ErrorCodes.UnknownApplication]: 'Unknown application',
  [ErrorCodes.AutomatedRequestsNotAllowed]: 'Automated requests are not allowed',
  [ErrorCodes.InvalidRating]: 'Invalid rating',
  [ErrorCodes.DuplicateBot]: 'Bot already exists',
  [ErrorCodes.CannotDetectIp]: 'Cannot detect requester ip. Looks like network is not configured properly',
  [ErrorCodes.ThirdPartyMonitoringFail]: 'Third party monitoring is not available',
  [ErrorCodes.ThirdPartyMonitoringNotApproved]: 'Third party monitoring is not approved your application',
  [ErrorCodes.TurnstileError]: 'Unknown turnstile captcha error',
  [ErrorCodes.ServiceConfiguredIncorrectly]: 'Service is configured incorrectly. Please contact support',
  [ErrorCodes.UnknownBoost]: 'Unknown boost',
  [ErrorCodes.BoostExpired]: 'Boost expired',
  [ErrorCodes.AlreadyCommented]: 'Cannot post a review twice',
  [ErrorCodes.ReviewRatingsConflict]: 'Server<->Server conflict in the rating. Comment in DB has too high rating',
  [ErrorCodes.TooManyConsecutiveMessages]: 'Too many consecutive messages in chat.',
  [ErrorCodes.NoServiceBot]: 'No service bot',
  [ErrorCodes.AlreadyReported]: 'Already reported',
  [ErrorCodes.CannotDeleteOwner]: 'Cannot delete owner from developers',
  [ErrorCodes.BlockedBot]: 'Bot is blocked, unblock it first',
  [ErrorCodes.UnknownAutomation]: 'Unknown automation',
  [ErrorCodes.OnlyOwnerCanAddServer]: 'Only owner can add server',
  [ErrorCodes.ReviewReported]: 'Review has not solved report',
}

export const httpCodes: Record<number, ErrorCodes> = {
  500: ErrorCodes.InternalServerError,
  429: ErrorCodes.RateLimited,
  404: ErrorCodes.NotFound,
  403: ErrorCodes.Forbidden,
  401: ErrorCodes.Unauthorized,
  400: ErrorCodes.BadRequest,
}

export const resolveCodeByHttp = (code: number) => {
  return httpCodes[code] ?? null
}
